package com.crm.controller;

import com.crm.annotation.LogOperation;
import com.crm.dto.BusinessDto;
import com.crm.dto.BusinessFollowDto;
import com.crm.entity.Business;
import com.crm.entity.Record;
import com.crm.result.PageResult;
import com.crm.result.PageResultTow;
import com.crm.result.Result;
import com.crm.service.BusinessService;
import com.crm.service.IDeptService;
import com.crm.service.RecordService;
import com.crm.vo.BusinessVo;
import com.crm.vo.DeptTreeVo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/business")
@Slf4j
@AllArgsConstructor
public class BusinessController {
    private final BusinessService businessService;
    private final RecordService recordService;
    private final IDeptService  deptService;

    /**
     * 新增商机
     *
     * @param business
     */
    @PostMapping
    public void insert(@RequestBody Business business) {
        log.info("新增商机{}", business);
        businessService.insert(business);
    }

    /**
     * 商机分页查询
     *
     * @param businessDto
     * @return
     */
    @LogOperation(title = "商机管理")
    @GetMapping("/list")
    public PageResultTow PageBusinessList(BusinessDto businessDto) {
        log.info("商机分页查询参数为，{}", businessDto);
        PageResultTow pageResult = businessService.PageBusinessList(businessDto);
        return pageResult;
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable("id") Integer id) {
        log.info("查询id为{}的商机", id);
        Business business = businessService.selectByBusinessId(id);
        return Result.success(business);
    }

    @PostMapping("/record")
    public Result insertRecord(@RequestBody BusinessFollowDto businessFollowDto) {
        log.info("新增商机跟进记录{}", businessFollowDto);
        recordService.insertRecord(businessFollowDto);
        return Result.success();
    }

    /**
     * 根据id查询商机跟进记录
     * @param businessId
     * @return
     */
    @GetMapping("/record/list")
    public Result getRecordsByBusinessId(@RequestParam("businessId") Integer businessId) {
        log.info("查询商机id为{}的跟进记录", businessId);
        Result pageResult = recordService.getRecordsByBusinessId(businessId);
        return pageResult;
    }

    /**
     * 将商机提回公海
     * @param business_id
     * @param reason
     * @return
     */
    @PutMapping("/back/{business_id}/{reason}")
    public Result putBusiness(@PathVariable Integer business_id, @PathVariable Integer reason) {
        log.info("将商机id为{}的商机提回公海", business_id, reason);
        businessService.putBusiness(business_id, reason);
        return Result.success();
    }

    @PutMapping("/assignment")
    public Result<List<DeptTreeVo>> getDeptTreeSelect() {
        List<DeptTreeVo> deptList = deptService.getDeptTreeList();
        return Result.success(deptList);
    }

}
